import pandas as pd
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
from statsmodels.tsa.arima.model import ARIMA
import seaborn as sns
import statsmodels.api as sm
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score
import matplotlib.pyplot as plt
import numpy as np

# 1. 数据预处理和清洗
# 读取CSV文件
df = pd.read_csv('../data/数据分析/Coffee_Chain_Sales.csv', nrows=20)
features = df[['Date', 'Market','AreaCode', 'Cogs', 'Margin', 'Profit', 'InventoryMargin']]
target = df['Sales']
print(f"{features}")

fig, ax = plt.subplots(figsize=(10, 6))
ax.axis('tight')
ax.axis('off')
# 绘制表格
the_table = ax.table(cellText=features.values, colLabels=features.columns, cellLoc='center', loc='center')
# 设置表格列宽为自适应
for j in range(len(features.columns)):
    the_table.auto_set_column_width(col=j)
# 设置表格属性
the_table.auto_set_font_size(False)
the_table.set_fontsize(10)
# 应用渐变颜色
num_rows = len(features)
for i in range(num_rows):
    for j in range(len(features.columns)):
        cell = the_table._cells[(i, j)]
plt.legend()
# plt.show()
plt.savefig('Sales1.png')  # 保存图像


df = pd.read_csv('../data/数据分析/Coffee_Chain_Sales.csv')
# 将Date列转换为datetime格式
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%Y')
# 提取年份、月份作为新特征
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
features = df[['Year', 'Month', 'AreaCode', 'Cogs', 'Margin', 'Profit', 'InventoryMargin']]
target = df['Sales']
print(f"{features}")
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# 将训练集和测试集标签添加到原始DataFrame中，以便于可视化时区分
df['Dataset'] = 'Not Selected'
df.loc[X_train.index, 'Dataset'] = 'Train'
df.loc[X_test.index, 'Dataset'] = 'Test'
# 使用seaborn绘制数据集的数据分布散点图以展示Cogs和Profit的分布
plt.figure(figsize=(10, 6))
sns.scatterplot(x='Cogs', y='Sales', hue='Dataset', data=df, alpha=0.7)
plt.title('Scatter Plot of Cogs and Sales for Train and Test Sets')
plt.xlabel('Cogs')
plt.ylabel('Sales')
plt.legend()
# plt.show()
plt.savefig('Sales2.png')  # 保存图像

# 绘制Sales绘制数据集的数据的分布图（直方图）
plt.figure(figsize=(10, 6))
sns.histplot(df['Sales'][df['Dataset'] == 'Train'], label='Train', alpha=0.7, bins=30, kde=True)
sns.histplot(df['Sales'][df['Dataset'] == 'Test'], label='Test', alpha=0.7, bins=30, kde=True)
plt.title('Distribution of Sales for Train and Test Sets')
plt.xlabel('Sales')
plt.ylabel('Frequency')
plt.legend()
# plt.show()
plt.savefig('Sales3.png')  # 保存图像
